<?php

namespace addons\booking\model;

use think\Model;


class CouponCondition extends Model
{


    // 表名
    protected $name = 'booking_coupon_condition';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text'
    ];


    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2'), '3' => __('Type 3'), '4' => __('Type 4')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    //获取指定房间或门店的优惠券条件id
    public static function getHouseCondition($house_id, $store_id)
    {
        return self::where('type', 'in', [3, 4, 5])->whereOr(function ($query) use ($house_id) {
            $house_id = (int)$house_id;
            $query->where('type', 1)->where("FIND_IN_SET('{$house_id}',content)");
        })->whereOr(function ($query) use ($store_id) {
            $store_id = (int)$store_id;
            $query->where('type', 2)->where("FIND_IN_SET('{$store_id}',content)");
        })->select();
    }

    //获取指定商品的优惠券条件id
    public static function getGoodsCondition($goods_ids)
    {
        return self::where('type', 'in', [3, 4])
            ->whereOr(function ($query) use ($goods_ids) {
                $sql = '1=2';
                foreach ($goods_ids as $id) {
                    $id = (int)$id;
                    $sql .= " OR FIND_IN_SET('{$id}',content)";
                }
                $query->where('type', 6)->where($sql);
            })->select();
    }
}
